package com.bds.btcdc.adapter.gateio.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bds.btcdc.model.CoinPairExchanges;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bds.btcdc.adapter.service.IAdapterService;
import com.bds.btcdc.http.HttpUtil;
import com.bds.btcdc.model.MarketData;
import com.bds.btcdc.api.model.Result;
import com.bds.btcdc.util.Const;

@Service
public class GateioService implements IAdapterService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	public final String PAIRS_URL = "/api2/1/pairs";

	public final String MARKETINFO_URL = "/api2/1/marketinfo";

	public final String MARKETLIST_URL = "/api2/1/marketlist";

	public final String TICKERS_URL = "/api2/1/tickers";

	public final String TICKER_URL = "/api2/1/ticker";

	public final String ORDERBOOK_URL = "/api2/1/orderBook";

	public final String BALANCE_URL = "/api2/1/private/balances";

	private final String DEPOSITADDRESS_URL = "/api2/1/private/depositAddress";

	private final String DEPOSITESWITHDRAWALS_URL = "/api2/1/private/depositsWithdrawals";

	private final String BUY_URL = "/api2/1/private/buy";

	private final String SELL_URL = "/api2/1/private/sell";

	private final String CANCELORDER_URL = "/api2/1/private/cancelOrder";

	private final String CANCELALLORDERS_URL = "/api2/1/private/cancelAllOrders";

	private final String GETORDER_URL = "/api2/1/private/getOrder";

	private final String OPENORDERS_URL = "/api2/1/private/openOrders";

	private final String TRADEHISTORY_URL = "/api2/1/tradeHistory";

	private final String WITHDRAW_URL = "/api2/1/private/withdraw";

	private final String MYTRADEHISTORY_URL = "/api2/1/private/tradeHistory";

	public Result getMarketInfo(List<CoinPairExchanges> giteio_pairs) {
		List<String> support_pairs=new ArrayList<>();
		for(CoinPairExchanges p:giteio_pairs){
			support_pairs.add(p.getPair_code());
		}
		Result ret = new Result();
		List<MarketData> ms = new ArrayList<MarketData>();
		try {
			HttpUtil httpUtil = HttpUtil.getInstance();
			String param = "";

			String result = httpUtil.requestHttpGet(Const.GATEIO.URL, TICKERS_URL, param);
			if (StringUtils.isNotEmpty(result)) {
				JSONObject json = JSONObject.parseObject(result);

				String[] keys = new String[json.keySet().size()];
				json.keySet().toArray(keys);
				for (String pair : keys) {
//					String pair = "btc_usdt";
					if(support_pairs.contains(pair)) {
						JSONObject data = json.getJSONObject(pair);
						MarketData market = new MarketData();
						market.setVol(data.getString("baseVolume"));
						market.setDayHigh(data.getString("high24hr"));
						market.setDayLow(data.getString("low24hr"));
						market.setHigh(data.getString("highestBid"));
						market.setLow(data.getString("lowestAsk"));
						market.setLast(data.getString("last"));
						market.setTimestamp(new Date().getTime());
						market.setPair(pair);
						market.setExchange("gateio");
						ms.add(market);
					}
				}
				ret.setCode(Const.SUC);
				ret.setData(ms);
			} else {
				ret.setCode(Const.FAIL_500);
				ret.setData(ms);
				ret.setMsg("没有此交易所数据gateio");
			}
		} catch (HttpException e) {
			logger.error("", e);
			ret.setCode(Const.FAIL);
			ret.setData(ms);
			ret.setMsg("解析协议异常：gateio");
		} catch (IOException e) {
			logger.error("", e);
			ret.setCode(Const.FAIL);
			ret.setData(ms);
			ret.setMsg("解析协议异常：gateio");
		}
		return ret;

	}
}
